﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meditime.Domain;
using Cqrs.Infrastructure;
using Cqrs.Infrastructure.Database;
using EventStore;
using System.Reflection;
using System.IO;
using domain = Meditime.Domain;

namespace Meditime.UI
{
    internal class EventStore
    {
        public static readonly string ShiftRecordingSmf = @"ShiftRecording.smf";
        public static readonly string StaffMemberSmf = @"StaffMember.smf";

        private static IEventStore _eventStore;
        private static readonly object lockObject = new object();

        private EventStore()
        {
        }

        public static IEventStore Instance()
        {
            if (_eventStore == null)
            {
                lock (lockObject)
                {
                    Cqrs.Infrastructure.EventPublisher publisher = new EventPublisher();
                    publisher.SynchronousPublishing = true;

                    // Staff Members
                    publisher.RegisterHandler<StaffMemberCreated>(EventHandlers.HandleEvent);
                    publisher.RegisterHandler<MadeFulltime>(EventHandlers.HandleEvent);
                    publisher.RegisterHandler<MadeContract>(EventHandlers.HandleEvent);
                    publisher.RegisterHandler<MarkedAsInactive>(EventHandlers.HandleEvent);
                    publisher.RegisterHandler<NameUpdated>(EventHandlers.HandleEvent);
                    publisher.RegisterHandler<DateOfBirthUpdated>(EventHandlers.HandleEvent);

                    //Shift Recording
                    publisher.RegisterHandler<AddedToRoster>(EventHandlers.HandleEvent);
                    publisher.RegisterHandler<StartedWorking>(EventHandlers.HandleEvent);
                    publisher.RegisterHandler<OnCallStarted>(EventHandlers.HandleEvent);
                    publisher.RegisterHandler<MarkedAsOffDuty>(EventHandlers.HandleEvent);
                    publisher.RegisterHandler<GoneOnHoliday>(EventHandlers.HandleEvent);
                    publisher.RegisterHandler<RemovedFromRoster>(EventHandlers.HandleEvent);

                    _eventStore = new RavenDb(
                        Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Db"),
                            publisher);
                }
            }
            return _eventStore;
        }
    }
}
